<script>
import {storageDelete, storageListPage, storageSave, storageUpdate} from "@/api/house";
import {   pcaTextArr} from 'element-china-area-data'

export default {
  name: "StorageManage",
  data() {
    return {

      options: pcaTextArr,
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      name: '',
      centerDialogVisible: false,
      selectedOptions: [],
      // addForm:{
      //
      // },
      form: {
        id: '',
        name: '',
        address: '',
        remark: '',
        // province: '',
        // county: '',
        // area: '',
      },
      rules: {
        name: [
          {required: true, message: '请输入仓库名', trigger: 'blur'},
        ],
        address: [
          {required: true, trigger: 'blur'},
        ],
        remark: [
          {required: true, message: '请输入详细地址', trigger: 'blur'},
        ],
      }
    }
  },
  methods: {
    addressChoose(value) {
      console.log(value)
      // this.selectedOptions = value
      // var name = ''
      // this.selectedOptions.map(item => name += codeToText[item] + '') //将省市区三个拼接一起
      this.form.address = value[0] + "/" + value[1] + "/" + value[2]
      // this.form.province = codeToText[this.selectedOptions[0]]
      // this.form.county = codeToText[this.selectedOptions[1]]
      // this.form.area = codeToText[this.selectedOptions[2]]
    },
    resetForm() {

      this.$refs.form.resetFields()

      this.form.id = ''

    },
    // loadPost() {
    //   this.$axios.post(this.$httpUrl + "/storage/listPage",
    //       {
    //         pageNum: this.pageNum, pageSize: this.pageSize, param: {
    //           name: this.name
    //         }
    //       }
    //   ).then(res => res.data).then(res => {
    //     console.log(res)
    //     if (res.code == 200) {
    //       this.tableData = res.data
    //       this.total = res.total
    //     } else {
    //       alert("数据获取失败")
    //     }
    //
    //   })
    // },
    loadPost() {
      storageListPage({
        pageNum: this.pageNum, pageSize: this.pageSize, param: {name: this.name}
      }).then(res => res.data).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.tableData = res.data
          this.total = res.total
        } else {
          alert("数据获取失败")
        }
      })
    },

    add() {
      this.centerDialogVisible = true
      this.$nextTick(() => {
        this.selectedOptions=[]
        this.resetForm()
      })
    },
    mod(row) {
      this.centerDialogVisible = true
      this.selectedOptions.push(row.address.split("/")[0], row.address.split("/")[1], row.address.split("/")[2])
      this.$nextTick(() => {
        //赋值到表单
        this.form.id = row.id
        this.form.name = row.name
        this.form.address = this.selectedOptions[0] + "/" + this.selectedOptions[1] + "/" + this.selectedOptions[2]
        this.form.remark = row.remark
      })
    },
    // 删除
    // del(id) {
    //   this.$axios.delete(this.$httpUrl + "/storage/delete?id=" + id
    //   ).then(res => res.data).then(res => {
    //     if (res.code == 200) {
    //       this.$message({
    //         message: '操作成功',
    //         type: 'success'
    //       })
    //       this.loadPost()
    //     } else {
    //       this.$message({
    //         message: '操作失败',
    //         type: 'error'
    //       });
    //     }
    //   })
    // },
    del(id) {
      storageDelete({id: id}).then(res => res.data).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.loadPost()
        } else {
          this.$message({
            message: '操作失败',
            type: 'error'
          });
        }
      })
    },
    // 更新
    // doMod() {
    //   this.$axios.post(this.$httpUrl + "/storage/update", this.form
    //   ).then(res => res.data).then(res => {
    //     console.log(res)
    //     if (res.code == 200) {
    //       this.$message({
    //         message: '操作成功',
    //         type: 'success'
    //       })
    //       this.centerDialogVisible = false
    //       this.loadPost()
    //     } else {
    //       this.$message({
    //         message: '操作失败',
    //         type: 'error'
    //       });
    //     }
    //   })
    // },
    doMod() {
      storageUpdate(this.form).then(res => res.data).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.centerDialogVisible = false
          this.loadPost()
        } else {
          this.$message({
            message: '操作失败',
            type: 'error'
          });
        }
      })
    },

    // 新增
    // doSave() {
    //   this.$axios.post(this.$httpUrl + "/storage/save", this.form
    //   ).then(res => res.data).then(res => {
    //     console.log(res)
    //     if (res.code == 200) {
    //       this.$message({
    //         message: '操作成功',
    //         type: 'success'
    //       })
    //       this.centerDialogVisible = false
    //       this.loadPost()
    //     } else {
    //       this.$message({
    //         message: '操作失败',
    //         type: 'error'
    //       });
    //     }
    //
    //   })
    // },
    doSave() {
      storageSave(this.form).then(res => res.data).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.centerDialogVisible = false
          this.loadPost()
        } else {
          this.$message({
            message: '操作失败',
            type: 'error'
          });
        }
      })
    },
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id) {
            this.doMod()
          } else {
            this.doSave()
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetParam() {
      this.name = ''
      this.loadPost()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum = 1
      this.pageSize = val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val
      this.loadPost()
    }
  },
  beforeMount() {
    this.loadPost()
  },
  // created() {
  //   this.options = regionData
  // },

}
</script>

<template>
  <div>
    <div style="margin-bottom: 4px">
      <el-input v-model="name" placeholder="请输入仓库名" suffix-icon="el-icon-search"
                style="width: 200px ;margin-right: 10px"
                @keyup.enter.native="loadPost"></el-input>
      <el-button type="primary" style="margin-left: 10px" @click="loadPost">查询</el-button>
      <el-button type="success" @click="resetParam">重置</el-button>
      <el-button type="primary" style="margin-left: 10px" @click="add">新增</el-button>
    </div>
    <el-table :data="tableData" :header-cell-style="{background:'#E6F1FF',color:'#368CFF'}" border>
      <el-table-column prop="id" label="ID" width="60">
      </el-table-column>
      <el-table-column prop="name" label="仓库名" width="180">
      </el-table-column>
      <el-table-column prop="address" label="地址" width="180">
      </el-table-column>
      <el-table-column prop="remark" label="详细地址" width="180">
      </el-table-column>
      <el-table-column prop="operate" label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)">
            <el-button slot="reference" size="small" type="danger" style="margin-left: 5px">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页-->
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2,5,10,20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog
        title="提示"
        :visible.sync="centerDialogVisible"
        width="30%"
        center>
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="仓库名" prop="name">
          <el-col :span="18">
            <el-input v-model="form.name"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-cascader
              style="width: 75%"
              filterable
              clearable
              :options="options"
              v-model="selectedOptions"
              @change="addressChoose"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="remark">
          <el-col :span="18">
            <el-input type="textarea" v-model="form.remark"></el-input>
          </el-col>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
    </span>
    </el-dialog>
  </div>
</template>


<style scoped>

</style>